<?php

//session_start();

class DirectMessagesController extends AppController {
	
	public $helpers = array('Html', 'Form');


	function index(){
	}
  
/*
 * Description of "jesenddm"
 * @param  "userid","accountid","sentTo" 
 *
 *This function is for the junior editor user type to send a direct message, it adds the message in the future_dms table with the characteristics of
 *a junior editor's message .
 *
 * @author "Ahmed El-Fouly" , "Mark Milad"
 */
public function senddm($postTo) {
		$this->loadModel('User');
		$accountID=$this->Auth->User('accID');
		$userID=$this->Auth->User('userID');
		$sentTo = $this->DirectMessage->query("select twitterName from accounts Account JOIN posts Post on Account.accID=Post.accID where Post.postID='".$postTo."'");
		//debug($postTo);
		//debug($sentTo);
		$v = $this->User->getPermissionLevel($accountID,$userID);
		//debug($v);
		//debug($userID);
		//debug($this->User->isAdmin($userID));
		if ($this->request->data) {
		
		
		$approval = $this->setapproval($userID, $v); //checks permission and returns false only for JE
	 
	 	$String_DirectMessageText = "".($this->request->data['DirectMessage']['Text']);
	    if(strlen($String_DirectMessageText)==0){
		$this->Session->setFlash('Write something First');
			}
			else{

				if(strlen($String_DirectMessageText)<=140){
					if($v == 2 || $v == 3 ||$v == 4 || $this->User->isAdmin($userID)){
						$request = $this->sendtotwitter($sentTo[0]['Account']['twitterName'] , $String_DirectMessageText);
						
						
					}
					else {
		       			$current_time = date('Y-m-d H:i:s');
						$this->request->data['future_dms']['messageText'] = $String_DirectMessageText;
						$this->request->data['future_dms']['userID'] = $userID;
						$this->request->data['future_dms']['sub_time'] = $current_time;
						$this->request->data['future_dms']['approved'] = $approval;
						$this->request->data['future_dms']['accID'] = $accountID;
						$this->request->data['future_dms']['sentTo'] = $sentTo[0]['Account']['twitterName'];
						$this->loadModel('future_dms');
						$l = $this->future_dms->save($this->request->data);
                		$this->Session->setFlash("Your Message will be sent after the moderator's approval");
                		$this->Redirect('/posts');
            	}
			}
				else{		
				$this->Session->setFlash('Your direct message exceeded the 140 chars');		
				}			
			}
					
	}
		else{
		$this->request->data['Tweet']['Text'] = $this->Session->Read('ReplyName');
		$_SESSION['ReplyName'] = null;	
			}
	}
	
	
	
	public function setapproval($userID , $permission){
	if( $permission == 2 || $permission == 3 ||$permission == 4 || $this->User->isAdmin($userID)){
	return true;
	}
	else {
	return false;
	}
	}
	
	
	/*
	* Description of function "sendtotwitter"
	* @param string $sn 
	* @param string $text 
	* 
	*
	* Description of the function:
	* This function takes some text and the screen name of a user and sends a direct message containing the text to the user with that username
	* @author "Mark Milad"
	*/
	public function sendtotwitter($sn , $text){
		$tempAccount = new Account();
		$accountID=$this->Auth->User('accID');
        $array = array();
        $request = $tempAccount->connectionRequest($accountID)->post('direct_messages/new', array('screen_name' => $sn , 'text' => $text));
		$this->Session->setFlash('Your Message Has been sent!');
		//echo "send to";
		//debug($sn);
		//echo "text: ";
		//debug($text);
		//echo "request";
		//debug($request);
		//debug($sn);
		//debug($text);
		$this->redirect('/posts');
		
	
	}
	
	
	


	 
	 
}